package net.xxcxcxcx.xc_enhancedmob.register.client;

import net.fabricmc.api.EnvType;
import net.fabricmc.api.Environment;
import net.fabricmc.fabric.api.client.networking.v1.ClientPlayNetworking;
import net.minecraft.entity.Entity;
import net.minecraft.entity.EntityType;
import net.minecraft.entity.mob.MobEntity;
import net.minecraft.text.Text;
import net.xxcxcxcx.xc_enhancedmob.EnhancedMobMod;
import net.xxcxcxcx.xc_enhancedmob.component.BaseComponent;
import net.xxcxcxcx.xc_enhancedmob.component.EnhancedBaseComponent;
import net.xxcxcxcx.xc_enhancedmob.data.MobData;
import net.xxcxcxcx.xc_enhancedmob.entity.SaveFireballEntity;
import net.xxcxcxcx.xc_enhancedmob.entity.SaveFireworkRocketEntity;
import net.xxcxcxcx.xc_enhancedmob.packet.CustomEntitySpawnPacket;
import net.xxcxcxcx.xc_enhancedmob.packet.CustomNbtPacket;
import net.xxcxcxcx.xc_enhancedmob.packet.CustomParticlePacket;
import net.xxcxcxcx.xc_enhancedmob.register.EntityRegister;
import net.xxcxcxcx.xc_enhancedmob.render.particle.summon.CustomSummonParticle;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.UUID;

@Environment(value = EnvType.CLIENT)
public class NetworkingRegister {
    public static final HashMap<Integer, List<Text>> moretitlepacket = new HashMap<>();
    public static final List<CustomEntitySpawnPacket> customentitylist = new ArrayList<>();

    public static final List<CustomSummonParticle> customParticlePackets = new ArrayList<>();
    public static final List<CustomNbtPacket> customMobNbt = new ArrayList<>();

    @Environment(value = EnvType.CLIENT)
    public static void ClientRegister() {
        //数据包本地接收，但是有时候会出现数据包发送早于生物实体数据包发送的时间，此时暂存在moretitlepacket里面，在生物实体加载时候统一处理
        ClientPlayNetworking.registerGlobalReceiver(EnhancedMobMod.MoreTitleIdentifier, (client, handler, buf, responseSender) -> {
            int id = buf.readInt();
            int i = buf.readInt();
            List<Text> list = new ArrayList<>();
            while (i-- > 0) {
                list.add(buf.readText());
            }
            if (client.world == null) {
                moretitlepacket.put(id, list);
                return;
            }
            Entity entity = client.world.getEntityById(id);
            if (entity == null) {
                moretitlepacket.put(id, list);
                return;
            }
            if (entity instanceof MobEntity mobEntity) {
                BaseComponent baseComponent = ((MobData) mobEntity).getComponent(EnhancedBaseComponent.class.getName());
                if (baseComponent instanceof EnhancedBaseComponent enhancedBaseComponent) {
                    enhancedBaseComponent.setMoreTitle(list);
                }
            }
        });
        //自定义生物的生成。
        ClientPlayNetworking.registerGlobalReceiver(EnhancedMobMod.CustomEntityIdentifier, (client, handler, buf, responseSender) -> {
            Entity entity2;
            Entity entity;
            CustomEntitySpawnPacket spawnPacket = new CustomEntitySpawnPacket(buf);
            if (client.world == null) {
                customentitylist.add(spawnPacket);
                return;
            }
            int id = spawnPacket.id;
            UUID uuid = spawnPacket.uuid;
            EntityType<?> entityType = spawnPacket.entityType;
            double x = spawnPacket.x;
            double y = spawnPacket.y;
            double z = spawnPacket.z;
            byte pitch = spawnPacket.pitch;
            byte yaw = spawnPacket.yaw;
            int entityData = spawnPacket.entityData;
            short velocityX = spawnPacket.velocityX;
            short velocityY = spawnPacket.velocityY;
            short velocityZ = spawnPacket.velocityZ;
            if (entityType == EntityRegister.SaveFireballEntityType) {
                entity = new SaveFireballEntity(client.world, x, y, z, velocityX, velocityY, velocityZ);
                entity2 = client.world.getEntityById(entityData);
                if (entity2 != null) {
                    ((SaveFireballEntity) entity).setOwner(entity2);
                }
            } else if (entityType == EntityRegister.SaveFireworkRocketType) {
                entity = new SaveFireworkRocketEntity(client.world, x, y, z, velocityX, velocityY, velocityZ);
                entity2 = client.world.getEntityById(entityData);
                if (entity2 != null) {
                    ((SaveFireworkRocketEntity) entity).setOwner(entity2);
                }
            } else entity = null;
            if (entity != null) {
                entity.updateTrackedPosition(x, y, z);
                entity.refreshPositionAfterTeleport(x, y, z);
                entity.pitch = (float) (pitch * 360) / 256.0f;
                entity.yaw = (float) (yaw * 360) / 256.0f;
                entity.setEntityId(id);
                entity.setUuid(uuid);
                client.world.addEntity(id, entity);
            }
        });
        //自定义粒子生成
        ClientPlayNetworking.registerGlobalReceiver(EnhancedMobMod.CustomParticle, (client, handler, buf, responseSender) -> {
            CustomParticlePacket packet = new CustomParticlePacket(buf);
            customParticlePackets.add(packet.Particle);
        });
        //自定义bossbar生成
        ClientPlayNetworking.registerGlobalReceiver(EnhancedMobMod.MoreNbtIdentifier, (client, handler, buf, responseSender) -> {
            CustomNbtPacket packet = new CustomNbtPacket(buf);
            customMobNbt.add(packet);
        });
    }
}
